package org.polaris.gift.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Objects;

@Component
public class SendMailUtil {
    @Autowired
    private JavaMailSender javaMailSender;

    public void send(String theme,String text,String addressee,Long shopId) {
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1527138015@qq.com");
        helper.setSubject(theme);
        if (Objects.isNull(shopId)) {
            helper.setText("<h1>"+text+"</h1>",true);
        }else{
            //      <a href=localhost:9999/shop/activate/60>点击激活</a>
            helper.setText(text,true);
        }
        //添加附件
        //helper.addAttachment("罗宾.jpg",new File("C:\\Users\\hm\\Desktop\\work\\aa.jpg"));
        //收件人
            helper.setTo(addressee);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        javaMailSender.send(mimeMessage);
    }

}
